const path = require("path");

// const ENV_NODE = (process.env.NODE_ENV || "").toLowerCase(); // development|production|testing;
const lifecycle = process.env.npm_lifecycle_script || "";
const variable = (lifecycle.match(/NODE_ENV=(\w+)/) || [])[1]; // development|production|testing;

const script = []; // 外部js
const noscript = []; // noscript异常
const meta = []; // 媒体

// 配置信息
const config = {
    NODE_ENV: /development|production|testing/.test(variable)
        ? variable
        : "development", // 环境变量
    DOMAIN_API: "", // vue接口地址
    DOMAIN_IMAGE: "", // 图片资源地址-美国
    DOMAIN_PROXY: "", // 代理地址
    // DOMAIN_DRAWER: "http://47.253.69.73:8000", // 生成图片的地址
};

// config.NODE_ENV = "production";

if (config.NODE_ENV == "production") {
    // 生产环境
    config.NODE_ENV = "production";
    config.DOMAIN_API = "https://www.podpartner.com/api"; // vue接口地址
    config.DOMAIN_PROXY = "https://www.podpartner.com/api"; // 代理地址
    config.DOMAIN_IMAGE = "https://img.podpartner.com"; // 图片资源地址-美国
    // DOMAIN_IMAGE: "https://pod-product-cn.oss-cn-shenzhen.aliyuncs.com", // 图片资源地址-深圳

    // Google Tag Manager
    script.push({
        innerHTML:
            "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-MJCCL7F');",
    });

    // AW-11125078886

    // Google tag (gtag.js)
    script.push({
        async: true,
        src: "https://www.googletagmanager.com/gtag/js?id=AW-10829089541",
    });

    // Google Tag Manager bind Google tags
    script.push({
        innerHTML:
            "window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);};gtag('js', new Date());gtag('config', 'AW-10829089541');gtag('config', 'AW-11125078886');gtag('config', 'GT-W6X7RBQ');gtag('config', 'G-S7W66EL5SF');",
    });

    //Facebook Pixel Code
    script.push({
        body: true,
        defer: true,
        async: true,
        innerHTML:
            "!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, document,'script','https://connect.facebook.net/en_US/fbevents.js');fbq('init', '680955222922957');fbq('track', 'PageView');",
    });

    // 第三方聊天工具
    script.push({
        body: true,
        defer: true,
        async: true,
        src: "//code-eu1.jivosite.com/widget/xUTyQvS0AU",
    });

    // noscript
    // Facebook Pixel Code
    noscript.push({
        body: true,
        innerHTML:
            '<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=680955222922957&ev=PageView&noscript=1" />',
    });

    meta.push({
        name: "facebook-domain-verification",
        content: "36ubsl2zeqc5dkt21q4kc5dzdr91n0",
    });
    meta.push({
        name: "google-site-verification",
        content: "qv6KzTTpV3hWPwVALjjjLbW2wN7-rRIIcRPHiDHLjQc",
    });

    // Google Tag Manager
    noscript.push({
        body: true,
        innerHTML:
            '<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-MJCCL7F"height="0" width="0" style="display:none;visibility:hidden"></iframe>',
    });
} else if (config.NODE_ENV == "testing") {
    // 测试环境
    config.NODE_ENV = "testing";
    // https://zxtest.podpartner.com/api
    // http://podtest-1707394846.us-east-2.elb.amazonaws.com/api
    config.DOMAIN_API = "https://zxtest.podpartner.com/api";
    config.DOMAIN_PROXY = "https://zxtest.podpartner.com/api";
    config.DOMAIN_IMAGE = "https://img.podpartner.com"; // 图片资源地址-美国

    meta.push({ name: "robots", content: "noindex" });
} else {
    // 开发环境环境
    config.NODE_ENV = "development";
    config.DOMAIN_API = "http://3.140.123.144:8080/api";
    config.DOMAIN_PROXY = "http://3.140.123.144:8080/api";
    config.DOMAIN_IMAGE = "https://img.podpartner.com"; // 图片资源地址-美国
}

export default {
    // Global page headers (https://go.nuxtjs.dev/config-head)
    loading: false,
    server: {
        port: 3000, // default: 3000
        host: "0.0.0.0", // default: localhost
    },
    serverMiddleware: [
        "@/middleware/server/request-format.js",
        "@/middleware/server/redirects",
        "@/middleware/server/drawer",
    ],
    // xxs
    render: {
        compressor: false, // gzip 压缩
        // csp: {
        //   hashAlgorithm: "sha256",
        //   allowedSources: undefined,
        //   policies: {
        //     "script-src": ["'self'", "*", "'unsafe-eval'", "'unsafe-inline'"],
        //     // "frame-ancestors": ["https://www.podpartner.com"],
        //   },
        // },
    },
    devServer: { https: true },
    head: {
        htmlAttrs: {
            lang: "en",
        },
        title: "PODpartner:Print-on-Demand Dropshipping & Start Your Own Online Store",
        meta: [
            { charset: "utf-8" },
            {
                name: "viewport",
                content:
                    "width=device-width,initial-scale=1,minimum-scale=1, maximum-scale=1, user-scalable=no",
            },
        ].concat(...meta),
        link: [
            // { rel: "icon", type: "image/x-icon", href: "/favicon.ico" },
            // {
            //   rel: "stylesheet",
            //   href: "//at.alicdn.com/t/font_2777279_c3kjtt7z88t.css",
            // }, //pod-web
            // {
            //   rel: "stylesheet",
            //   href: "//at.alicdn.com/t/font_2930587_w5xqlw5nsq.css",
            // }, //pod-center
            // {
            //   rel: "stylesheet",
            //   href: "//at.alicdn.com/t/font_2901707_ut860re2ip.css",
            // }, //pod-design
        ],
        script: script,
        noscript: noscript,
        __dangerouslyDisableSanitizers: ["script", "noscript"],
    },

    // Global CSS (https://go.nuxtjs.dev/config-css)
    css: [
        "@/assets/styles/font-family.scss",
        "@/assets/styles/global.scss",
        "@/assets/iconfont/iconfont.css",
    ],

    // Plugins to run before rendering page (https://go.nuxtjs.dev/config-plugins)
    plugins: [
        { src: "~/plugins/http.js", ssr: true },
        { src: "~/plugins/plugins.js", ssr: true },
        { src: "~/plugins/eventbus.js", ssr: false },
        { src: "~/plugins/upload-file.js", ssr: false },
        { src: "~/plugins/vue-lazyload.js", ssr: false },
        { src: "~/plugins/vue-element.js", ssr: false },
        { src: "~/plugins/vue-fabric.js", ssr: false },
        { src: "~/plugins/store-localstorage.js", ssr: false },
        { src: "~/plugins/icons.js", ssr: true },
        { src: "~/plugins/gme-repore.js", ssr: false },
        { src: "~/tool/message/index.js", ssr: false },
    ],

    // Auto import components (https://go.nuxtjs.dev/config-components)
    components: true,

    // Modules for dev and build (recommended) (https://go.nuxtjs.dev/config-modules)
    // buildModules: [
    //   '@aceforth/nuxt-optimized-images',
    // ],
    // optimizedImages: {
    //   optimizeImages: true
    // },
    env: {
        // ...process.env,
        ...config,
    },

    // Modules (https://go.nuxtjs.dev/config-modules)
    modules: [
        "@nuxtjs/axios",
        "@nuxtjs/proxy",
        ["cookie-universal-nuxt", { alias: "cookiz" }],
    ],
    // 表示开启代理
    axios: { proxy: true },
    proxy: {
        "/zx": { target: config.DOMAIN_PROXY },
        "/pod": { target: config.DOMAIN_PROXY },
        "/data": { target: config.DOMAIN_PROXY },
    },
    // Build Configuration (https://go.nuxtjs.dev/config-build)
    build: {
        // 生成报告，仅仅在分析时用，真正打包不要开
        // analyze: true,
        // 低于10kb做base64处理
        loaders: { imgUrl: { limit: 20000 } },

        // 按需引入
        babel: {
            plugins: [
                [
                    "component",
                    {
                        libraryName: "element-ui",
                        styleLibraryName: "theme-chalk",
                    },
                ],
            ],
        },

        // 提取css到单独link文件
        extractCSS: false,

        // 拓展
        extend(config, ctx) {
            const svgRule = config.module.rules.find((rule) =>
                rule.test.test(".svg")
            );
            svgRule.exclude = [path.resolve(__dirname, "assets/svgs")];
            // Includes /icons/svg for svg-sprite-loader
            config.module.rules.push({
                test: /\.svg$/,
                include: [path.resolve(__dirname, "assets/svgs")],
                loader: "svg-sprite-loader",
                options: {
                    symbolId: "icon-[name]",
                },
            });
        },
    },
};
